Today you will
Set up your coding environment, run your first programs, and learn to read error messages.
- Set up the course environment by typing the commands yourself (House Cleaning).
- Run a Python file from the terminal and from Thonny.
- Use
print(), text in quotes, comments, and maths inside print. - Read the last line of an error and fix SyntaxError, IndentationError and NameError.
House Cleaning: set up your environment
Every programmer sets up their workspace before they code. Type each command yourself in the terminal, inside your course folder. Stuck for more than 5 minutes? Double-click SETUP-Windows.bat in the Starter Kit and ask for help.
Windows (PowerShell)
| Type this | What it does |
|---|---|
py --version | Checks Python is installed. You want 3.12 or newer. |
py -m venv $HOME\python-course-env | Builds your private Python environment. WHY? A mini sandbox: it contains our code and protects global settings and other apps/code |
& $HOME\python-course-env\Scripts\Activate.ps1 | Switches it on. The prompt starts with (python-course-env). activate the Virtual Environment |
python -m pip install --upgrade pip | Updates pip. pip is like the Playstore for Python: it downloads and installs packages (extra tools) for us. pip stands for "Pip Installs Packages". |
pip install streamlit | Downloads Streamlit. Streamlit will be our shortcut for our apps to run in the browser, its free and cheaper for now. later courses that is focused on app making will introduce other options, such as pyqt, TKinter, React etc |
python setup_check.py | Every line should say OK. - test that streamlit and python is installed. we can just do the python --version or the streamlit --version instead to show the version confirming the installed status of the packages. |
mkdir $HOME\.streamlit -Force | Makes a folder for Streamlit's settings. |
notepad $HOME\.streamlit\config.toml | Opens a new settings file. Type the settings below and save. you can make this manually inside the folder. |
streamlit run app.py | Starts the test app. Click the button: YAY, it works! |
If Activate.ps1 says running scripts is disabled, skip activating and run: & $HOME\python-course-env\Scripts\python.exe -m streamlit run app.py --server.address 127.0.0.1
Mac and Ubuntu Linux (Terminal)
| Type this | What it does |
|---|---|
python3 --version | Checks Python is installed. |
python3 -m venv ~/python-course-env | Builds your private Python environment. (Ubuntu first: sudo apt install python3-venv) |
source ~/python-course-env/bin/activate | Switches it on. |
pip install --upgrade pip | Updates pip. |
pip install streamlit | Downloads Streamlit. |
python setup_check.py | Every line should say OK. |
mkdir -p ~/.streamlit | Makes a folder for Streamlit's settings. |
nano ~/.streamlit/config.toml | Opens a new settings file. Type the settings, then Ctrl+O, Enter, Ctrl+X. |
streamlit run app.py | Starts the test app. Click the button: YAY, it works! |
The settings file
[server]
address = "127.0.0.1"
[browser]
gatherUsageStats = falseaddress = "127.0.0.1": only your computer can open your app, at http://127.0.0.1:8501. gatherUsageStats = false: Streamlit doesn't send usage information.
1. Your first program
Open 01_hello.py in Thonny and run it with the green Run button (or press F5). You can also run it from the terminal with python 01_hello.py.
# My first Python program
# Lines that start with # are comments: notes for people. Python ignores them.
print("Hello, world!")
print("I am learning Python")# give it a variable (a name that stores a value - more on variables in Module 2)
my_math = 3 + 5
print(my_math)
#print the results directly
print(3 + 9)
# printing a prefix to the calculation
print("4 + 2 =", 4 + 2)8
12
4 + 2 = 62. The shop sign
The start of the shop program we'll build all course.
# shop.py - Module 1: the shop's welcome sign
# We'll keep adding to this program every module.
print("=" * 32) # "=" repeated 32 times makes a border
print(" Welcome to Mama J's Bakery")
print(" Fresh patties every morning")
print("=" * 32)
print("Beef Patty EC$5.00")
print("Cheese Patty EC$5.00")
print("Coconut Tart EC$4.00")
print("-" * 32)
print("2 beef patties cost EC$", 2 * 5) # Python can do maths inside print
print("Open 7am - 6pm, Kingstown")
print("=" * 32)Words to know
| Word | Meaning |
|---|---|
print() | Shows something on the screen |
| String | Text inside quotes: "Hello" |
| Comment | A note starting with #. Python ignores it. WHY we use comments? For other people to understand your code faster, for you to remember what you wanted the code to do especially if you run into errors later on. |
| Error | Python's message that something is wrong. Read the last line first |
| Case-sensitive | print and Print are different to Python |
| Virtual environment | A private Python folder for one project |
Bug hunt
This program has 4 mistakes. Run it, read the last line of the error, fix that one mistake, and run it again.
# BUG HUNT - this program has 4 mistakes.
# 1. Run it. 2. Read the LAST line of the error. 3. Fix that one mistake.
# 4. Run it again. Repeat until the sign prints.
print("Welcome to the shop)
print("Today's special: Coconut Tart")
print("Patties are EC$5.00"
Print("Thank you for visiting")
print("Come again!")Hints (open only if stuck)
- Every piece of text needs a quote at the start and the end.
- Brackets come in pairs.
- Lines don't start with spaces (yet).
- Python is case-sensitive.
Practice
Homework 1 (5%): my_sign.py
A sign for your own business, a family business, or one you'd like to start. It must include:
Submit your .py file and a screenshot of it running, before the next module.
| Mark | Means |
|---|---|
| 5 | Runs and has everything |
| 3 | Runs with parts missing, or has errors |
| 1 | Started |
| 0 | Not submitted |
Quiz 1
Check yourself here, then take the graded quiz in the quiz section of robotics.a3tech.uk (two tries).
ADDITIONAL - STREAMLIT APP - SNEAK PEEK
# shop_app.py - the shop's welcome sign, as a Streamlit app
# Run it with: streamlit run shop_app.py
import streamlit as st # bring in Streamlit and call it "st"
st.title("Welcome to Mama J's Bakery") # big heading at the top
st.subheader("Fresh patties every morning") # smaller heading
st.divider() # a line across the page (our border)
st.write("Beef Patty: EC$5.00")
st.write("Cheese Patty: EC$5.00")
st.write("Coconut Tart: EC$4.00")
st.divider()
st.write(f"2 beef patties cost EC${2 * 5}.00") # the f and { } let you put maths inside text
st.caption("Open 7am - 6pm, Kingstown") # small grey text at the bottomSee the browser for the Streamlit appYour mission today
Make your robot talk, drive, stop, go backwards and spin, using real Python code!
Log in
- Go to robotics.a3tech.uk and open Python Mode.
- Use your callsign (your code name), never your real name.
Your first robot code
Copy this into Python Mode and press RUN CODE.
# Module 1 - Meet your robot
# Paste this into Python Mode on robotics.a3tech.uk and press RUN CODE.
print("Robot online!")
speed = 50 # a variable: a name that stores a number
robot.set_motors(speed, speed) # left wheel, right wheel: both forward
robot.wait(1000) # keep going for 1000 ms = 1 second
robot.stop()
print("Mission complete")Robot words
| Code | What it means |
|---|---|
print("Hi") | The robot says Hi |
robot.set_motors(50, 50) | Both wheels go forward |
robot.wait(1000) | Keep going for 1 second (1000 ms) |
robot.stop() | Stop the wheels |
speed = 50 | A variable: a name that stores a number |
Challenges
Try one at a time. Change the code, run it, and watch.
# Module 1 - Robot challenges
# Try one at a time. Change the code, press RUN CODE, watch what happens.
# Challenge 1: drive twice as far. Hint: change the wait time.
robot.set_motors(50, 50)
robot.wait(1000)
robot.stop()
# Challenge 2: drive BACKWARDS. Hint: negative numbers go backwards.
# Challenge 3: spin on the spot. Hint: one wheel forward, one wheel backward.
# Challenge 4: make the robot say three things with print().Build puzzle
These lines are mixed up. Put them in order so the robot says hello, drives for 1 second and stops.
robot.stop()
robot.wait(1000)
print("Robot online!")
robot.set_motors(50, 50)Show the answer
print("Robot online!")
robot.set_motors(50, 50)
robot.wait(1000)
robot.stop()Bug alert
This line has a mistake. Can you find it?
print("Mission complete)Show the answer
The closing quote is missing:
print("Mission complete")Quiz 1
Pass with 3 out of 5. Then take it for points in the quiz section of robotics.a3tech.uk.